<template>
<div>
  <el-row  class="do-exam-title" style="background-color: #F5F5DC">
    <el-col :span="24">
        <span :key="item.itemOrder"  v-for="item in answer.answerItems">
             <el-tag :type="questionDoRightTag(item.doRight)" class="do-exam-title-tag" @click="goAnchor('#question-'+item.itemOrder)">{{item.itemOrder}}</el-tag>
        </span>
    </el-col>
  </el-row>
  <el-row  class="do-exam-title-hidden">
    <el-col :span="24">
        <span :key="item.itemOrder"  v-for="item in answer.answerItems">
             <el-tag  class="do-exam-title-tag" >{{item.itemOrder}}</el-tag>
        </span>
    </el-col>
  </el-row>
  <el-container  class="app-item-contain">
    <el-header class="align-center">
      <h1>{{form.name}}</h1>
      <div>
        <span class="question-title-padding">试卷得分：{{answer.score}}</span>
        <span class="question-title-padding">试卷耗时：{{formatSeconds(answer.doTime)}}</span>
      </div>
    </el-header>
    <el-main>
      <el-form :model="form" ref="form" v-loading="formLoading" label-width="100px">
        <el-row :key="index"  v-for="(titleItem,index) in form.titleItems">
          <h3>{{titleItem.name}}</h3>
          <el-card class="exampaper-item-box" v-if="titleItem.questionItems.length!==0">
            <el-form-item :key="questionItem.itemOrder" :label="questionItem.itemOrder+'.'"
                          v-for="questionItem in titleItem.questionItems"
                          class="exam-question-item"  label-width="50px" :id="'question-'+ questionItem.itemOrder">
              <el-row>
                <QuestionAnswerShow :qType="questionItem.questionType" :question="questionItem"  :answer="answer.answerItems[questionItem.itemOrder-1]"/>
              </el-row>
              <el-row v-if="answer.answerItems[questionItem.itemOrder-1].doRight === null">
                <label style="color: #e6a23c">批改：</label>
                <el-radio-group v-model="answer.answerItems[questionItem.itemOrder-1].score">
                  <el-radio  v-for="item in scoreSelect(questionItem.score)"  :key="item"  :label="item" >
                    {{item}}
                  </el-radio>
                </el-radio-group>
              </el-row>
            </el-form-item>
          </el-card>
        </el-row>

        <el-row v-if="answer.videoUrl">

          <h3>编程视频</h3>

          <el-card>
            <el-form-item label="评分：" prop="videoScore">
              <el-select v-model="answer.videoScore" placeholder="视频评分">
                <el-option v-for="item in videoScoreEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <!--<video :src="answer.videoUrl"></video>-->
              <aliplayer  class="aliplayer_box" ref="player" @liveStreamStop="liveStreamStop($event)" :autoplay="false" :isLive="isLive" :rePlay="false" showBuffer="false" showBarTime="5000" format="m3u8"
                         :source="answer.videoUrl"></aliplayer>
            </el-form-item>
          </el-card>
        </el-row>
        <br/>
        <el-row class="do-align-center">
          <el-button type="primary" @click="submitForm">提交</el-button>
          <!--<el-button>取消</el-button>-->
        </el-row>
      </el-form>
    </el-main>
  </el-container>
</div>
</template>

<script>
import aliplayer from '../components/Aliplayer' // 引入
import { mapState, mapGetters } from 'vuex'
import { formatSeconds } from '@/utils'
import QuestionAnswerShow from '../components/QuestionAnswerShow'
import correctApi from '@/api/correct'
export default {
  components: { QuestionAnswerShow, aliplayer },
  data () {
    return {
      isLive: false,
      visitable: false,
      form: {
        videoScore: null
      },
      formLoading: false,
      answer: {
        id: null,
        score: 0,
        doTime: 0,
        answerItems: [],
        doRight: false
      }
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      correctApi.read(id).then(re => {
        _this.form = re.response.paper
        _this.answer = re.response.answer
        _this.formLoading = false
      })
    }
  },
  methods: {
    // 直播播放器
    init(isLive){
      this.isLive = this.isLive
      this.visitable = true
    },
    liveStreamStop(event) {
      // message('直播失败或直播已结束')
      this.visitable = event
    },
    submitForm () {
      let _this = this

      if (this.answer.videoUrl && !this.answer.videoScore) {
        this.$message.error('请给视频评分')
        return
      }
      _this.formLoading = true
      correctApi.edit(this.answer).then(re => {

        if (re.code === 1) {
          _this.$alert('试卷得分：' + re.response + '分', '考试结果', {
            confirmButtonText: '返回答卷列表',
            callback: action => {
              _this.$router.push('/answer/list')
            }
          })
        } else {
          _this.$message.error(re.message)
        }
        _this.formLoading = false
      }).catch(e => {
        _this.formLoading = false
      })
    },
    scoreSelect (score) {
      let array = []
      for (let i = 0; i <= parseInt(score); i++) {
        array.push(i.toString())
      }
      if (score.indexOf('.') !== -1) {
        array.push(score)
      }
      return array
    },
    formatSeconds (theTime) {
      return formatSeconds(theTime)
    },
    questionDoRightTag (status) {
      return this.enumFormat(this.doRightTag, status)
    },
    goAnchor (selector) {
      this.$el.querySelector(selector).scrollIntoView({ behavior: 'instant', block: 'center', inline: 'nearest' })
    }
  },
  computed: {
    ...mapGetters('enumItem', ['enumFormat']),
    ...mapState('enumItem', {
      doRightTag: state => state.exam.question.answer.doRightTag,
      videoScoreEnum: state => state.exam.examPaper.videoScoreEnum
    })
  }
}
</script>

<style lang="scss" scoped>
  .align-center {
    text-align: center
  }

  .exam-question-item{
    padding: 10px;
    .el-form-item__label{
      font-size: 15px !important;
    }
  }

  .question-title-padding{
    padding-left: 25px;
    padding-right: 25px;
  }
</style>
